package com.king.pattem.pool;

import java.util.Random;

import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;

/**
 * @author taomk
 * @version 1.0
 * @since 15-2-9 下午4:58
 */
public class CommonsPoolTest {

	public static void main(String [] args) throws Exception {
		testStringPool();
		testUserPool();
		testRandomPool();
	}

	public static void testStringPool() throws Exception {
		// 创建一个对象池
		GenericKeyedObjectPool pool = new GenericKeyedObjectPool(new BaseKeyedPoolableObjectFactory() {
			@Override
			public Object makeObject(Object o) throws Exception {
				return o;
			}
		});

		// 添加对象到池，重复的不会重复入池
		pool.addObject("a");
		pool.addObject("a");
		pool.addObject("b");
		pool.addObject("x");

		// 清除最早的对象
		pool.clearOldest();

		// 获取并输出对象
		System.out.println(pool.borrowObject("a"));
		System.out.println(pool.borrowObject("b"));
		System.out.println(pool.borrowObject("c"));
		System.out.println(pool.borrowObject("c"));
		System.out.println(pool.borrowObject("a"));

		// 输出池状态
		System.out.println(pool.getMaxIdle());
		System.out.println(pool.getMaxActive());
	}

	public static void testUserPool() throws Exception {
		// 创建一个对象池
		GenericKeyedObjectPool pool = new GenericKeyedObjectPool(new BaseKeyedPoolableObjectFactory() {
			@Override
			public Object makeObject(Object o) throws Exception {
				if (o != null && o instanceof User)
					return o;
				else
					return null;
			}
		});

		// 添加对象到池，重复的不会重复入池
		pool.addObject("a");
		pool.addObject("b");
		pool.addObject("x");
		pool.addObject(null);
		pool.addObject(null);
		pool.addObject(null);
		pool.addObject(new User("zhangsan", "123"));
		pool.addObject(new User("lisi", "112"));
		pool.addObject(new User("lisi", "112", 32));
		pool.addObject(new User("lisi", "112", 32, "一个烂人！"));


		// 清除最早的对象
		pool.clearOldest();

		// 获取并输出对象
		User u1 = new User("lisi", "112", 32, "一个烂人！");
		System.out.println(pool.borrowObject(u1));
		pool.returnObject(u1,u1);

		// 获取并输出对象
		User u2 = new User("lisi", "112", 32, "一个烂人！");
		System.out.println(pool.borrowObject(u2));
		pool.returnObject(u2,u2);

		// 获取并输出对象
		User u3 = new User("lisi", "112", 32);
		System.out.println(pool.borrowObject(u3));
		pool.returnObject(u3,u3);

		// 获取并输出对象
		User u4 = new User("lisi", "112");
		System.out.println(pool.borrowObject(u4));
		pool.returnObject(u4,u4);

		System.out.println(pool.borrowObject(u4));
		pool.returnObject(u4,u4);
		System.out.println(pool.borrowObject(u4));
		pool.returnObject(u4,u4);
		System.out.println(pool.borrowObject(u4));
		pool.returnObject(u4,u4);
		System.out.println(pool.borrowObject(u4));
		pool.returnObject(u4,u4);
		System.out.println(pool.borrowObject(u4));
		pool.returnObject(u4,u4);
		System.out.println(pool.borrowObject(u4));
		pool.returnObject(u4,u4);
		System.out.println(pool.borrowObject(u4));
		pool.returnObject(u4,u4);

		System.out.println(pool.borrowObject(new User("lisi", "112")));

		// 输出池状态
		System.out.println(pool.getMaxIdle());
		System.out.println(pool.getMaxActive());
		pool.clearOldest();
		pool.close();
	}

	public static void testRandomPool() throws Exception {
		GenericObjectPool pool = new GenericObjectPool(new BasePoolableObjectFactory() {
			public Object makeObject() throws Exception {
				return new Random().nextFloat();
			}
		});

		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();
		pool.addObject();

		Object o = pool.borrowObject();
		System.out.println(o);
		pool.returnObject(o);

		Object o1 = pool.borrowObject();
		System.out.println(o1);

		Object o2 = pool.borrowObject();
		System.out.println(o2);

		Object o3 = pool.borrowObject();
		System.out.println(o3);

		Object o4 = pool.borrowObject();
		System.out.println(o4);


		Object o5 = pool.borrowObject();
		System.out.println(o5);


		Object o6 = pool.borrowObject();
		System.out.println(o6);

		Object o7 = pool.borrowObject();
		System.out.println(o7);

		Object o8 = pool.borrowObject();
		System.out.println(o8);

		pool.returnObject(o1);
		pool.returnObject(o2);
		pool.returnObject(o3);
		pool.returnObject(o4);
		pool.returnObject(o5);
		pool.returnObject(o6);
		pool.returnObject(o7);
		pool.returnObject(o8);

		Object o9 = pool.borrowObject();
		System.out.println(o9);
		pool.returnObject(o9);

		Object o10 = pool.borrowObject();
		System.out.println(o10);
		pool.returnObject(o10);

		Object o11 = pool.borrowObject();
		System.out.println(o11);
		pool.returnObject(o11);

		Object o12 = pool.borrowObject();
		System.out.println(o12);
		pool.returnObject(o12);

		Object o13 = pool.borrowObject();
		System.out.println(o13);
		pool.returnObject(o13);
	}
}
